﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IFT.Classes;

namespace IFT
{
  public partial class FormMain : Form
  {
    public delegate void OnErrorHandler(string message);

    private Battlefield m_battlefield;

    public FormMain()
    {
      InitializeComponent();
    }

    public void OnErrorMessage(string msg)
    {
      if (msgLogWindow.InvokeRequired)
        BeginInvoke(new MethodInvoker(delegate() { OnErrorMessage(msg); }));
      else
        msgLogWindow.Text = msg;
    }

    private void CreateBattlefield()
    {
      m_battlefield = new Battlefield(BattlefieldType.Town);
      TextBox txtUnitCount = null;
      System.Collections.IEnumerator iUnitCount = groupAttacker.Controls.GetEnumerator();

      while (iUnitCount.MoveNext())
      {
        txtUnitCount = (TextBox)iUnitCount.Current;
        m_battlefield.Attacker.AddUnit(txtUnitCount.TabIndex, Convert.ToInt32(txtUnitCount.Text));
      }

      txtUnitCount = null;
      iUnitCount = groupDefender.Controls.GetEnumerator();

      while (iUnitCount.MoveNext())
      {
        txtUnitCount = (TextBox)iUnitCount.Current;
        m_battlefield.Defender.AddUnit(txtUnitCount.TabIndex, Convert.ToInt32(txtUnitCount.Text));
      }
    }

    private void txtUnitCount_KeyPress(object sender, KeyPressEventArgs e)
    {
      if(!Char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar))
        e.Handled = true;
    }

    private Label PrepareLabel(int id, string unitName)
    {
      Label newLabel = new Label();
      newLabel.Anchor = AnchorStyles.Left | AnchorStyles.Top;
      newLabel.Location = new Point(12, groupAttacker.Location.Y - groupAttacker.Margin.All + (26 * id));
      newLabel.Name = "lblUnit" + Convert.ToString(id);
      newLabel.AutoSize = true;
      newLabel.Size = new Size(newLabel.PreferredWidth, newLabel.PreferredHeight);
      newLabel.TabIndex = id;
      newLabel.Text = unitName + ":";
      return newLabel;
    }

    private TextBox PrepareTxtBox(int id, string groupName)
    {
      TextBox newTxtBox = new TextBox();
      newTxtBox.Anchor = AnchorStyles.Left | AnchorStyles.Top;
      newTxtBox.Location = new Point(6, 19 + (26 * (id - 1)));
      newTxtBox.Name = "txt" + groupName + "Unit" + Convert.ToString(id);
      newTxtBox.Size = new Size(60, 20);
      newTxtBox.TabIndex = id;
      newTxtBox.Text = "0";
      newTxtBox.MaxLength = 6;
      newTxtBox.KeyPress += new KeyPressEventHandler(this.txtUnitCount_KeyPress);
      tabOptions.Size = new Size(tabOptions.Size.Width, groupAttacker.Height + (newTxtBox.Margin.All * 2) + newTxtBox.Height + (newTxtBox.Margin.All * 2));
      btnStartSimulation.Location = new Point(tabOptions.Width / 2 - btnStartSimulation.Width / 2, tabOptions.Height + btnStartSimulation.Margin.All);
      return newTxtBox;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      foreach (XMLUnit unit in UnitManager.Instance.Units)
      {
        tabUnitCount.Controls.Add(PrepareLabel(unit.ID, unit.Name));
        groupAttacker.Controls.Add(PrepareTxtBox(unit.ID, "Attacker"));
        groupDefender.Controls.Add(PrepareTxtBox(unit.ID, "Defender"));
      }
    }

    private void btnStartSimulation_Click(object sender, EventArgs e)
    {
      CreateBattlefield();
      m_battlefield.NextRound();
      string lol = "";
      lol = "bar";
    }
  }
}
